﻿using DG.Tweening;
using Gp.Scripts.Core;
using QFramework;
using UnityEngine;
using UnityEngine.UI;


namespace Gp.Scripts.Core {
    public class SaveSelectPanelController : MonoBehaviour {
        [SerializeField] private Transform container;
        [SerializeField] private Button closeButton;
        [SerializeField] private SavePanelButton prefab;
        [SerializeField] private CanvasGroup canvasGroup;
        
        private bool _opened = false;

        private void Start() {
            closeButton.onClick.AddListener(Hide);
        }



        public void Show() {
            if (_opened) return;
            _opened = true;
            canvasGroup.DOFade(1, 0.2f);
            canvasGroup.interactable = true;
            canvasGroup.blocksRaycasts = true;
            
            foreach (var fileName in Global.Get<SaveManager>().GetSaveFiles()) {
                var button = Instantiate(prefab, container);
                button.Init(fileName);
            }
        }


        public void Hide() {
            _opened = false;
            container.DestroyChildren();
            canvasGroup.DOFade(0, 0.2f);
            canvasGroup.interactable = false;
            canvasGroup.blocksRaycasts = false;
        }
        
    }
}